package com.cyj.dream.admin.controller.admin.system;

import com.alibaba.fastjson.JSONObject;
import com.cyj.dream.admin.model.system.SysModule;
import com.cyj.dream.admin.service.system.SysModuleService;
import com.cyj.dream.core.aspect.annotation.ResponseResult;
import com.cyj.dream.core.aspect.annotation.log.LogRecord;
import com.cyj.dream.core.aspect.enums.OperatorType;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import com.cyj.dream.core.util.IsEmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 系统菜单模块控制器
 * @BelongsProject: fckj-restaurant
 * @BelongsPackage: com.fckj.fckjrestaurant.controller.admin.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-04-29 10:25
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@RestController
@ResponseResult
@Api(value = "module", tags = "菜单模块")
@RequestMapping(value = "/pc/module", name = "菜单模块")
public class SysModuleController {

    @Autowired
    private SysModuleService sysModuleService;

    /**
     * 查询菜单模块树
     *
     * @param sysModule 菜单对象
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/30 10:35
     * @author ChenYongJia
     */
    @ApiOperation(value = "查询菜单模块树", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysModule", value = "菜单对象", dataType = "SysModule", required = false)
    })
    @LogRecord(title = "查询菜单模块树", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/selectSysModuleTree", name = "查询菜单模块树", method = RequestMethod.POST)
    public Map<String, Object> selectSysModuleTree(@RequestBody SysModule sysModule) {
        log.info("进入查询菜单模块树控制器，参数为：{}", JSONObject.toJSONString(sysModule));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysModule)) {
            log.error("出现错误：查询菜单模块树失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("data", sysModuleService.selectSysModuleTree(sysModule));
        return map;
    }

    /**
     * 根据用户查询的左侧权限菜单栏
     *
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/30 10:35
     * @author ChenYongJia
     */
    @ApiOperation(value = "根据用户查询的左侧权限菜单栏", httpMethod = "GET")
    @LogRecord(title = "根据用户查询的左侧权限菜单栏", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/selectParentSysModule", name = "根据用户查询的左侧权限菜单栏", method = RequestMethod.GET)
    public Map<String, Object> selectParentSysModule() {
        log.info("进入根据用户查询的左侧权限菜单栏控制器");
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);

        map.put("router", sysModuleService.selectParentSysModule());
        return map;
    }

    /**
     * 根据模块id查询模块信息
     *
     * @param sysModuleId 菜单模块id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/30 10:35
     * @author ChenYongJia
     */
    @ApiOperation(value = "根据模块id查询模块信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysModuleId", value = "菜单模块id", dataType = "Long", required = true)
    })
    @LogRecord(title = "根据模块id查询模块信息", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/selectModuleByModuleId", name = "根据模块id查询模块信息", method = RequestMethod.GET)
    public Map<String, Object> selectModuleByModuleId(Long sysModuleId) {
        log.info("进入根据模块id查询模块信息控制器，参数为：{}", sysModuleId);
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysModuleId)) {
            log.error("出现错误：根据模块id查询模块信息失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("data", sysModuleService.selectModuleByModuleId(sysModuleId));
        return map;
    }

    /**
     * 添加菜单模块信息
     *
     * @param sysModule 菜单模块对象
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-4-30 11:21:31
     * @author ChenYongJia
     */
    @ApiOperation(value = "添加菜单模块信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysModule", value = "菜单模块对象", dataType = "SysModule", required = true)
    })
    @LogRecord(title = "添加菜单模块信息", operatetype = OperatorType.INSERT)
    @RequestMapping(value = "/addModuleInfo", name = "添加菜单模块信息", method = RequestMethod.POST)
    public Map<String, Object> addModuleInfo(@RequestBody SysModule sysModule) {
        log.info("进入添加菜单模块信息控制器，参数为：{}", JSONObject.toJSONString(sysModule));
        this.CheckData(sysModule);

        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        map.put("data", sysModuleService.addModuleInfo(sysModule));
        return map;
    }

    /**
     * 修改菜单模块信息
     *
     * @param sysModule 菜单模块对象
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-4-30 11:21:31
     * @author ChenYongJia
     */
    @ApiOperation(value = "修改菜单模块信息", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysModule", value = "菜单模块对象", dataType = "SysModule", required = true)
    })
    @LogRecord(title = "修改菜单模块信息", operatetype = OperatorType.UPDATE)
    @RequestMapping(value = "/updateModuleInfo", name = "修改菜单模块信息", method = RequestMethod.PUT)
    public Map<String, Object> updateModuleInfo(@RequestBody SysModule sysModule) {
        log.info("进入修改菜单模块信息控制器，参数为：{}", JSONObject.toJSONString(sysModule));
        this.CheckData(sysModule);

        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        map.put("data", sysModuleService.updateModuleInfo(sysModule));
        return map;
    }

    /**
     * 删除菜单模块信息
     *
     * @param sysModule 菜单模块对象
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-4-30 11:21:31
     * @author ChenYongJia
     */
    @ApiOperation(value = "删除菜单模块信息", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysModule", value = "菜单模块对象", dataType = "SysModule", required = true)
    })
    @LogRecord(title = "删除菜单模块信息", operatetype = OperatorType.DELETE)
    @RequestMapping(value = "/deleteModuleInfo", name = "删除菜单模块信息", method = RequestMethod.DELETE)
    public Map<String, Object> deleteModuleInfo(@RequestBody SysModule sysModule) {
        log.info("进入删除菜单模块信息控制器，参数为：{}", JSONObject.toJSONString(sysModule));
        if (IsEmptyUtils.isEmpty(sysModule.getDreamId())) {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        map.put("data", sysModuleService.deleteModuleInfo(sysModule));
        return map;
    }

    /**
     * 根据模块id查询模块信息
     *
     * @param sysModuleId 模块id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-5-19 14:37:39
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "根据模块id查询模块信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysModuleId", value = "模块id", dataType = "Long", required = true)
    })
    @LogRecord(title = "根据模块id查询模块信息", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/findBySysModuleId", name = "根据模块id查询模块信息", method = RequestMethod.GET)
    public Map<String, Object> findBySysModuleId(Long sysModuleId) {
        log.info("进入根据模块id查询模块信息方法控制器，参数为 sysModuleId：{}", sysModuleId);
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysModuleId)) {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        SysModule sysModule = new SysModule();
        sysModule.setDreamId(sysModuleId);
        map.put("info", sysModuleService.selectBySysModuleName(sysModule));
        return map;
    }

    /**
     * 用于统一check菜单对象中必须携带的参数数据
     *
     * @param sysModule 菜单模块对象
     * @return void
     * @date 2021/4/30 11:26
     * @author ChenYongJia
     */
    public void CheckData(SysModule sysModule) {
        if (IsEmptyUtils.isEmpty(sysModule.getSysModuleName()) || IsEmptyUtils.isEmpty(sysModule.getSysModuleWeight())) {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
    }

}
